package com.ztmg.water.service.base.impl;

import com.ztmg.water.model.base.Supplier;
import com.ztmg.water.vo.base.SupplierVO;
import com.ztmg.water.dto.base.SupplierDTO;
import com.ztmg.water.dto.base.SupplierPageDTO;
import com.ztmg.common.util.resultUtils.JsonResult;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ztmg.common.constant.ErrorConst;
import org.springframework.beans.BeanUtils;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import com.ztmg.water.service.base.SupplierService;
import com.ztmg.water.mapper.base.SupplierMapper;import com.ztmg.water.model.base.SupplierExample;

@Service
public class SupplierServiceImpl implements SupplierService{

    @Resource
    private SupplierMapper supplierMapper;
    @Override
    public JsonResult create(SupplierDTO dto) {
        if(null != dto){
            Supplier bean = new Supplier();
            BeanUtils.copyProperties(dto,bean);
            bean.setCreateTime(new Date());
            //后续完善
            bean.setCreater("");
            supplierMapper.insert(bean);
            return JsonResult.success();
        }else {
            return JsonResult.fail(ErrorConst.SYSTEM_PARM_ERROR,ErrorConst.SYSTEM_PARM_ERROR_MSG);
        }
    }

    @Override
    public JsonResult update(SupplierDTO dto) {
        if(null != dto && null != dto.getId()){
            Supplier bean = new Supplier();
            BeanUtils.copyProperties(dto,bean);
            bean.setModifyTime(new Date());
            //后续完善
            bean.setModifier("");
            supplierMapper.updateByPrimaryKeySelective(bean);
            return JsonResult.success();
        }else {
            return JsonResult.fail(ErrorConst.SYSTEM_PARM_ERROR,ErrorConst.SYSTEM_PARM_ERROR_MSG);
        }
    }

    @Override
    public JsonResult<SupplierVO> selectById(Integer id) {
        if(null != id){
            Supplier bean = supplierMapper.selectByPrimaryKey(id);
            SupplierVO vo = new SupplierVO();
            if(null != bean){
                BeanUtils.copyProperties(bean,vo);
            }else {
                return JsonResult.fail(ErrorConst.SYSTEM_PARM_ERROR,ErrorConst.SYSTEM_PARM_ERROR_MSG);
            }
            return JsonResult.success(vo);
        }else{
            return JsonResult.fail(ErrorConst.SYSTEM_PARM_ERROR,ErrorConst.SYSTEM_PARM_ERROR_MSG);
        }
    }

    @Override
    public JsonResult<List<SupplierVO>> select(SupplierDTO dto) {
        SupplierExample example = new SupplierExample();
        SupplierExample.Criteria criteria =  example.createCriteria();
        /***
        * 根据具体的业务，查询条件赋值
         */
        List<Supplier> sysUserList = supplierMapper.selectByExample(example);

        List<SupplierVO> respDTOS = sysUserList.parallelStream().map((bean) -> {
            SupplierVO vo = new SupplierVO();
            BeanUtils.copyProperties(bean, vo);
            return vo;
        }).collect(Collectors.toList());
        return JsonResult.success(respDTOS);
    }

    @Override
    public JsonResult<PageInfo<SupplierVO>> selectPage(SupplierPageDTO dto) {
        SupplierExample example = new SupplierExample();
        SupplierExample.Criteria criteria =  example.createCriteria();
        /***
        * 根据具体的业务，查询条件赋值
         */
        PageHelper.startPage(dto.getPage(), dto.getLimit());
        List<Supplier> beanList = supplierMapper.selectByExample(example);
        PageInfo pageInfo = new PageInfo<>(beanList);
        List<SupplierVO> respDTOS = beanList.parallelStream().map((bean) -> {
            SupplierVO vo = new SupplierVO();
            BeanUtils.copyProperties(bean, vo);
            return vo;
        }).collect(Collectors.toList());
        pageInfo.setList(respDTOS);
        return JsonResult.success(pageInfo);
    }

    @Override
    public JsonResult deleteById(Integer id) {
        return JsonResult.success( supplierMapper.deleteByPrimaryKey(id));
    }

    @Override
    public JsonResult delete(SupplierDTO dto) {
        SupplierExample example = new SupplierExample();
        SupplierExample.Criteria criteria =  example.createCriteria();
        /***
        * 根据具体的业务，查询条件赋值
         */
        return JsonResult.success(supplierMapper.deleteByExample(example));
    }
}